<?php
include 'admin_passport_permission_lib.php';

$a_groupteams = $a_permissions = $a_users = array();

// <editor-fold defaultstate="collapsed" desc="Get Data">
foreach ($a_PhanBan as $key => $value) {
    
    $o_tmp                 = new stdClass();
    $o_tmp->groupid        = $key;
    $o_tmp->groupname      = $value;
    $o_tmp->groupreference = 'TEAM';
    
    $a_groupteams[$key] = $o_tmp;
}

$db_query = "select userid, saint, username, usertype from user where $s_get_HUYNH_TRUONG_condition";
$db_result = db_query($db_conn, $db_query);

while ($d_row = db_fetch($db_result)){
    $a_users[$d_row->userid] = $d_row;
}

if ($s_Action == 'update') {
    
    if($f_moduleactionid == ''){ raise_error('No ID.'); }
    
    $s_Where   = "moduleactionid = $f_moduleactionid";
    
    $db_query  = db_get_query($a_tablecolumns_namhoa_passport_module_action) . " where $s_Where limit 1";;
    $db_result = db_query($db_conn, $db_query);
    
    if (!($d_passport_module_action = db_fetch($db_result))) { raise_error('Select None'); }

    $db_query  = db_get_query($a_tablecolumns_namhoa_passport_permission) . ' where ' . $s_Where;
    $db_result = db_query($db_conn, $db_query);

    while ($d_passport_permission = db_fetch($db_result)) {
        $a_permissions[$d_passport_permission->permissionfortype][$d_passport_permission->permissionfor] = $d_passport_permission->permissionfor;
    }
}
// </editor-fold>

$a_js_files = array ( 'namhoa.form' );

include 'lib_head.php';
?>
<form action="admin_passport_permission_execute.php" method="post" id="form1">
    <div class="tabs-item-list" id="tabs-item-1">
        
    <div class="portlet-body">
        <table class="table table-bordered table-hover">
            <tr class="permission_contain_row permission_DEPARTMENT_TEAM">
                <td>
                    <div class="field">
                        <label>Team</label> 
                        <div class="rightside divlist">
                            <fieldset>
                                <legend>
                                    <input type="checkbox" class="choose_item parent_item"/>
                                    <b>All</b>
                                    <input type="text" onkeydown="search(this);" onkeyup="search(this);" value="" class="search" style="width: 175px;"/>  
                                </legend>
                                <ul>
                                    <?php foreach ($a_groupteams as $v_key => $o_value) { ?>
                                    <li><input type="checkbox" value="<?= check_plain($v_key) ?>" class="choose_item item check-with-label" name="permissions[userteams][]" id="allowgroups_<?= check_plain($v_key) ?>" <?= isset($a_permissions['GROUP'][$v_key]) ? ' checked="checked"' : ''?>/> <label class="label-for-check" for="allowgroups_<?= check_plain($v_key) ?>"><span class="name"><?= $o_value->groupname ?></span></label></li>
                                    <?php } ?>
                                </ul>
                            </fieldset>
                        </div>
                    </div> 
                </td>
            </tr>
            <tr>
                <td>
                    <div class="field">
                        <label>User</label> 
                        <div class="rightside divlist">
                            <fieldset>
                                <legend>
                                    <input type="checkbox" class="choose_item parent_item"/>
                                    <b>All</b>
                                    <input type="text" onkeydown="search(this);" onkeyup="search(this);" value="" class="search" style="width: 175px;"/>  
                                </legend>
                                <ul>
                                    <?php foreach ($a_users as $v_key => $o_user) { ?>
                                    <li>
                                        <input type="checkbox" value="<?= check_plain($v_key) ?>" class="choose_item item check-with-label" name="permissions[users][]" id="allowusers_<?= check_plain($v_key) ?>" <?= isset($a_permissions['USER'][$v_key]) ? ' checked="checked"' : ''?>/>
                                        <label class="label-for-check" for="allowusers_<?= check_plain($v_key) ?>">
                                            <span class="name"><?= $o_user->saint ?> <?= $o_user->username ?></span>
                                        </label>
                                    </li>
                                    <?php } ?>
                                </ul>
                            </fieldset>
                        </div>
                    </div> 
                </td>
            </tr
            <tr>
                <td></td>
            </tr>
            <tr>
                <td>
                    <div class="field">
                        <?= $s_InputString ?>
                        <input type="hidden" name="moduleactionid" id="moduleactionid" value="<?= check_plain($f_moduleactionid) ?>"/>
                        <input type="hidden" name="action" id="action" value="<?= check_plain($s_Action) ?>"/>
                        <label>&ensp;</label>
                        <input type="submit" class="button" value="Submit"/>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    </div>
</form>
<script type="text/javascript">
    /*<![CDATA[*/
    function search($this){
        var $_value   = $($this).val();
        var $_regexp  = new RegExp($_value, 'i');
        var $_parent  = $($this).closest('.rightside');
        
        $('li, option', $_parent).hide().filter(function($_idx, $_el) {
            return $_regexp.test($('input[type=checkbox]', $_el).val()) || $_regexp.test($('span.name', $_el).html());
        }).show();
    }
    /* ]]> */
</script>
<?php include 'lib_tail.php'; ?>